# Replication code for Taylor C. Boas and Amy Erica Smith, “Looks Like Me, Thinks Like Me: Descriptive Representation and Opinion Congruence in Brazil.” Latin American Research Review 54, 2 (2019).

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file reads the Brazil Latinobarómetro files, merges in external data, and writes new files to the working directory. We recommend running R replication files in the following order; please see readme.txt for details.
# 	1_merge_lapop.R
# 	2_merge_latinobarometro.R
# 	3_recode_reshape.R
# 	4_difference_in_distributions.R
# 	5_regressions.R
# 	6_civil_society_meeting.R
# 	7_mass_descriptives.R
# 	8_elite_descriptives.R
# 	9_mean_differences.R
#	10_difference_in_distributions_ks.R
#	11_elite_sample_simulation.R
#	12_converts_vs_lifelong.R

# Set working directory as appropriate
# setwd('~/Dropbox/brazil_leg_surveys/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.

rm(list=ls(all=T))

library(foreign)

load('data_rosettastone.RData')

# Load Latinbarómetro files for 2002-2010. These are subsets of the original files used for analysis (downloaded November 2013), containing the data only for Brazil and only for those variables used in the analysis. As of June 2019, the versions of these files publicly available on the Latinbarómetro website have changed, and the analysis cannot be exactly replicated using the new versions. See readme.txt for details.

load('lb_brazil02.RData')
load('lb_brazil03.RData')
load('lb_brazil04.RData')
load('lb_brazil05.RData')
load('lb_brazil06.RData')
load('lb_brazil07.RData')
load('lb_brazil08.RData')
load('lb_brazil09.RData')
load('lb_brazil10.RData')

brazil02<-lb_brazil02
brazil03<-lb_brazil03
brazil04<-lb_brazil04
brazil05<-lb_brazil05
brazil06<-lb_brazil06
brazil07<-lb_brazil07
brazil08<-lb_brazil08
brazil09<-lb_brazil09
brazil10<-lb_brazil10

# Load Latinbarómetro files for 2011 and 2013. Please visit http://www.latinobarometro.org/lat.jsp to download these files and put them in the working directory. We have confirmed that the replication code works with the versions of these files downloaded in early June 2019. If the filenames that you download differ from those below, the data files may have changed, and some revision of the code may be necessary to replicate the published results. 

latino2011<-read.dta('Latinobarometro_2011_esp.dta')
latino2013<-read.dta('Latinobarometro2013.dta')
latino2013spss<-read.spss('Latinobarometro2013.sav',to.data.frame=T)

brazil11<-latino2011[latino2011$idenpa=='Brazil',]
brazil13<-latino2013[latino2013$idenpa=='Brazil',]
brazil13spss<-latino2013spss[latino2013spss$IDENPA=='Brazil',]

# Annoyingly, only the 2013 SPSS file has the city name labels.
brazil13$ciudad<-as.character(brazil13spss$CIUDAD)

# Municipio: for 2003 and 2004, data files as downloaded do not contain municipio labels, but online analysis tool does; hence, generating crosstabs using the online analysis tool and the data to uniquely identify municipios

citycode.mostpower.03<-matrix(as.numeric(c(rownames(table(brazil03$ciudad,brazil03$p10st_1)),as.vector(apply(table(brazil03$ciudad,brazil03$p10st_1,brazil03$wt),c(1,2),function(x) round(sum(x*sort(unique(brazil03$wt))),0))))),ncol=10)
colnames(citycode.mostpower.03)<-c('citycode',colnames(table(brazil03$ciudad,brazil03$p10st_1)))
city.mostpower.03<-read.csv('city_mostpower_crosstab.csv',as.is=T,fileEncoding='MacRoman')
names(city.mostpower.03)<-tolower(gsub('\\.',' ', names(city.mostpower.03)))
colnames(citycode.mostpower.03)<-gsub('/',' ',tolower(iconv(colnames(citycode.mostpower.03),from='latin1')))
names(city.mostpower.03)<-gsub('no contesta','ns nr', names(city.mostpower.03))
city.mostpower.03<-merge(city.mostpower.03, citycode.mostpower.03, all=T)

city.mostpower.03$region<-gsub('BR: (.*)\\-.*', '\\1',city.mostpower.03$muni)
city.mostpower.03$muni<-gsub('BR: .*\\-(.*)', '\\1', city.mostpower.03$muni)
brazil03$region<-city.mostpower.03$region[match(brazil03$ciudad, city.mostpower.03$citycode,incomparables=NA)]
brazil03$muni<-city.mostpower.03$muni[match(brazil03$ciudad, city.mostpower.03$citycode,incomparables=NA)]

citycode.childwealth.04<-matrix(as.numeric(c(rownames(table(brazil04$ciudad,brazil04$p9stc)),as.vector(table(brazil04$ciudad,brazil04$p9stc)))),ncol=14)
colnames(citycode.childwealth.04)<-c('citycode',colnames(table(brazil04$ciudad,brazil04$p9stc)))
city.childwealth.04<-read.csv('city_childwealth_crosstab.csv',as.is=T,fileEncoding='MacRoman')
names(city.childwealth.04)<-gsub('X','', names(city.childwealth.04))
city.childwealth.04<-merge(city.childwealth.04, citycode.childwealth.04,all=T)

# Codes 21 and 22 in the data file appear to correspond to the single municipality of Contagem in the online crosstab. Contagem borders on Belo Horizonte. Assuming that 21 is Belo Horizonte (14 sampled, which is consistent with other large cities outside of Rio and SP) and 22 is Contagem (7 sampled, consistent with other suburbs like São Bernardo do Campo)

city.childwealth.04$muni[city.childwealth.04$citycode==21]<-'BR: Sudeste-Belo Horizonte'
city.childwealth.04$muni[city.childwealth.04$citycode==22]<-'BR: Sudeste-Contagem'
city.childwealth.04<-city.childwealth.04[!is.na(city.childwealth.04$citycode),]

city.childwealth.04$region<-gsub('BR: (.*)\\-.*', '\\1',city.childwealth.04$muni)
city.childwealth.04$muni<-gsub('BR: .*\\-(.*)', '\\1', city.childwealth.04$muni)
brazil04$region<-city.childwealth.04$region[match(brazil04$ciudad, city.childwealth.04$citycode,incomparables=NA)]
brazil04$muni<-city.childwealth.04$muni[match(brazil04$ciudad, city.childwealth.04$citycode,incomparables=NA)]

brazil05$muni<-sapply(strsplit(as.character(iconv(brazil05$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil06$muni<-sapply(strsplit(as.character(iconv(brazil06$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil07$muni<-sapply(strsplit(as.character(iconv(brazil07$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil08$muni<-sapply(strsplit(as.character(iconv(brazil08$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil09$muni<-sapply(strsplit(as.character(iconv(brazil09$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil10$muni<-sapply(strsplit(as.character(iconv(brazil10$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil11$muni<-sapply(strsplit(as.character(iconv(brazil11$ciudad,from='latin1')),'\\-'),function(x) x[2])
brazil13$muni<-sapply(strsplit(brazil13$ciudad,'\\-'),function(x) x[2])

# Merging in TSE codes

# 2002

brazil02$ciudad[brazil02$ciudad=='4204204']<-'4204202' # Apparent erroneous code; picking the one that is closest
brazil02$muni<-br$mun_datafolha[match(brazil02$ciudad,br$codeibge)]
brazil02$tse_code<-br$codetse[match(brazil02$ciudad,br$codeibge)]
brazil02$uf<-br$state[match(brazil02$ciudad,br$codeibge)]

# 2003

sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil03$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))
munimatches03<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil03$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha))==x))
brazil03$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil03$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))]
sapply(unique(munimatches03[which(sapply(munimatches03,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil03$tse_code[brazil03$muni=='Tabatinga']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Tabatinga AM']
brazil03$tse_code[brazil03$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil03$tse_code[brazil03$muni=='Pilar']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Pilar AL']
brazil03$tse_code[brazil03$muni=='Santo André']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santo André SP']
brazil03$tse_code[brazil03$muni=='Cachoeirinha']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Cachoeirinha RS']
brazil03$tse_code[brazil03$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']

brazil03$uf<-br$state[match(brazil03$tse_code,br$codetse)]

# 2004

sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil04$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))
munimatches04<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil04$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha))==x))
brazil04$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil04$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))]
sapply(unique(munimatches04[which(sapply(munimatches04,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil04$tse_code[brazil04$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil04$tse_code[brazil04$muni=='Santo André']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santo André SP']
brazil04$tse_code[brazil04$muni=='Ipueiras']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Ipueiras CE']
brazil04$tse_code[brazil04$muni=='Iguatu']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Iguatu PR']
brazil04$tse_code[brazil04$muni=='Santa Rita']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santa Rita PB']
brazil04$tse_code[brazil04$muni=='Paulista']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Paulista PE']
brazil04$tse_code[brazil04$muni=='Anchieta']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Anchieta ES']
brazil04$tse_code[brazil04$muni=='Cascavel']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Cascavel PR']
brazil04$tse_code[brazil04$muni=='Pinhalzinho']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Pinhalzinho SC']

brazil04$uf<-br$state[match(brazil04$tse_code,br$codetse)]

# 2005

brazil05$muni<-gsub('´',"'",brazil05$muni)
sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil05$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))
munimatches05<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil05$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha))==x))
brazil05$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil05$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))]
sapply(unique(munimatches05[which(sapply(munimatches05,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil05$tse_code[brazil05$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil05$tse_code[brazil05$muni=='Santo André']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santo André SP']
brazil05$tse_code[brazil05$muni=='Paulista']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Paulista PE']
brazil05$tse_code[brazil05$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil05$tse_code[brazil05$muni=='Santarém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santarém PA']
brazil05$tse_code[brazil05$muni=='Palmas']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Palmas TO']
brazil05$tse_code[brazil05$muni=='Bonito']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Bonito PE']
brazil05$tse_code[brazil05$muni=='Cantagalo']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Cantagalo RJ']
brazil05$tse_code[brazil05$muni=='São Carlos']<-br$codetse[paste(br$mun_datafolha,br$state) == 'São Carlos SP']
brazil05$tse_code[brazil05$muni=='Novo Horizonte']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Novo Horizonte SP']

brazil05$uf<-br$state[match(brazil05$tse_code,br$codetse)]

# 2006

brazil06$muni[brazil06$muni=='Pindaré']<-'Pindaré-Mirim'
brazil06$muni[brazil06$muni=='Bezerro']<-'Bezerros'
brazil06$muni[brazil06$muni=='Moji Das Cruzes']<-'Mogi Das Cruzes'
sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil06$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))
munimatches06<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil06$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha))==x))
brazil06$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil06$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))]
sapply(unique(munimatches06[which(sapply(munimatches06,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil06$tse_code[brazil06$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil06$tse_code[brazil06$muni=='Santo André']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santo André SP']
brazil06$tse_code[brazil06$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil06$tse_code[brazil06$muni=='Taperoá']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Taperoá BA']
brazil06$tse_code[brazil06$muni=='Morrinhos']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Morrinhos CE']
brazil06$tse_code[brazil06$muni=='Mesquita']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Mesquita RJ']
brazil06$tse_code[brazil06$muni=='São Vicente']<-br$codetse[paste(br$mun_datafolha,br$state) == 'São Vicente SP']

brazil06$uf<-br$state[match(brazil06$tse_code,br$codetse)]

# 2007

sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil07$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))
munimatches07<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil07$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha))==x))
brazil07$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil07$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))]
sapply(unique(munimatches07[which(sapply(munimatches07,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil07$tse_code[brazil07$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil07$tse_code[brazil07$muni=='Santo André']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santo André SP']
brazil07$tse_code[brazil07$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil07$tse_code[brazil07$muni=='Santa Luzia']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santa Luzia MG']
brazil07$tse_code[brazil07$muni=='Arapuá']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Arapuá MG']
brazil07$tse_code[brazil07$muni=='Boa Esperança']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Boa Esperança MG']
brazil07$tse_code[brazil07$muni=='Rio Claro']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Rio Claro SP']

brazil07$uf<-br$state[match(brazil07$tse_code,br$codetse)]

# 2008

brazil08$muni[brazil08$muni=='Biritiba']<-'Biritiba-Mirim'
brazil08$muni[brazil08$muni=='Moji Das Cruzes']<-'Mogi Das Cruzes'
sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil08$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))
munimatches08<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil08$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha))==x))
brazil08$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil08$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun_datafolha)))]
sapply(unique(munimatches08[which(sapply(munimatches08,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil08$tse_code[brazil08$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil08$tse_code[brazil08$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil08$tse_code[brazil08$muni=='Rio Claro']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Rio Claro SP']
brazil08$tse_code[brazil08$muni=='Primavera']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Primavera PA']
brazil08$tse_code[brazil08$muni=='Presidente Médici']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Presidente Médici RO']
brazil08$tse_code[brazil08$muni=='Filadélfia']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Filadélfia TO']
brazil08$tse_code[brazil08$muni=='Santa Inês']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santa Inês MA']
brazil08$tse_code[brazil08$muni=='Antônio Carlos']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Antônio Carlos MG']
brazil08$tse_code[brazil08$muni=='Tapira']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Tapira PR']
brazil08$tse_code[brazil08$muni=='Toledo']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Toledo PR']

brazil08$uf<-br$state[match(brazil08$tse_code,br$codetse)]

# 2009

sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil09$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))
munimatches09<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil09$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun))==x))
brazil09$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil09$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))]
sapply(unique(munimatches09[which(sapply(munimatches09,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil09$tse_code[brazil09$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil09$tse_code[brazil09$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil09$tse_code[brazil09$muni=='Filadélfia']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Filadélfia TO']
brazil09$tse_code[brazil09$muni=='Cachoeirinha']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Cachoeirinha RS']
brazil09$tse_code[brazil09$muni=='Paulista']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Paulista PE']
brazil09$tse_code[brazil09$muni=='Candeias']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Candeias MG']

brazil09$uf<-br$state[match(brazil09$tse_code,br$codetse)]

# 2010

sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil10$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))
munimatches10<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil10$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun))==x))
brazil10$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil10$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))]
sapply(unique(munimatches10[which(sapply(munimatches10,length)>1)]),function(x) paste(br$mun_datafolha,br$state)[x])

# After manual inspection of multiple matches:
brazil10$tse_code[brazil10$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil10$tse_code[brazil10$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil10$tse_code[brazil10$muni=='Filadélfia']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Filadélfia TO']
brazil10$tse_code[brazil10$muni=='São Gonçalo Do Amarante']<-br$codetse[paste(br$mun_datafolha,br$state) == 'São Gonçalo do Amarante CE']
brazil10$tse_code[brazil10$muni=='Queimadas']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Queimadas PB']
brazil10$tse_code[brazil10$muni=='Prata']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Prata MG']
brazil10$tse_code[brazil10$muni=='Santo André']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santo André SP']
brazil10$tse_code[brazil10$muni=='Aparecida']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Aparecida SP']
brazil10$tse_code[brazil10$muni=='São Simão']<-br$codetse[paste(br$mun_datafolha,br$state) == 'São Simão SP']
brazil10$tse_code[brazil10$muni=='Cascavel']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Cascavel PR']
brazil10$tse_code[brazil10$muni=='Alvorada']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Alvorada RS']

brazil10$uf<-br$state[match(brazil10$tse_code,br$codetse)]

# 2011

brazil11$muni[brazil11$muni=='Moji Das Cruzes']<-'Mogi Das Cruzes'
brazil11$muni[brazil11$muni=='Pariquera']<-'Pariquera Açu'
sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil11$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))
munimatches11<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil11$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun))==x))
brazil11$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil11$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))]
sapply(unique(munimatches11[which(sapply(munimatches11,length)>1)]),function(x) paste(br$mun,br$state)[x])

# After manual inspection of multiple matches:
brazil11$tse_code[brazil11$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil11$tse_code[brazil11$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']
brazil11$tse_code[brazil11$muni=='Candeias']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Candeias MG']
brazil11$tse_code[brazil11$muni=='Santa Inês']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santa Inês MA']
brazil11$tse_code[brazil11$muni=='Tangará']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Tangará SC']

brazil11$uf<-br$state[match(brazil11$tse_code,br$codetse)]

# 2013
brazil13$muni[brazil13$muni=='Porto Velho_duplicated_76001049']<-'Porto Velho'
sum(!chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil13$muni)) %in% chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))
munimatches13<-sapply(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil13$muni)),function(x) which(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun))==x))
brazil13$tse_code<-br$codetse[match(chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(brazil13$muni)), chartr("ÁÂÃÀÄÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇ","AAAAAEEEEIIIIOOOOOUUUUC",toupper(br$mun)))]
sapply(unique(munimatches13[which(sapply(munimatches13,length)>1)]),function(x) paste(br$mun,br$state)[x])

# After manual inspection of multiple matches:
brazil13$tse_code[brazil13$muni=='Belém']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Belém PA']
brazil13$tse_code[brazil13$muni=='Santa Luzia']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Santa Luzia MG']
brazil13$tse_code[brazil13$muni=='Cascavel']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Cascavel PR']
brazil13$tse_code[brazil13$muni=='Praia Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Praia Grande SP']
brazil13$tse_code[brazil13$muni=='Rio Claro']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Rio Claro SP']
brazil13$tse_code[brazil13$muni=='Campo Grande']<-br$codetse[paste(br$mun_datafolha,br$state) == 'Campo Grande MS']

brazil13$uf<-br$state[match(brazil13$tse_code,br$codetse)]

# Save files with merged in TSE codes
save(brazil02,file='brazil02.RData')
save(brazil03,file='brazil03.RData')
save(brazil04,file='brazil04.RData')
save(brazil05,file='brazil05.RData')
save(brazil06,file='brazil06.RData')
save(brazil07,file='brazil07.RData')
save(brazil08,file='brazil08.RData')
save(brazil09,file='brazil09.RData')
save(brazil10,file='brazil10.RData')
save(brazil11,file='brazil11.RData')
save(brazil13,file='brazil13.RData')

